import os
import winreg
import winshell


"""
这里调用了winshell的CreateShortcut函数。
传入4个参数，分别为：快捷方式的路径，exe文件的路径，图标路径，还有描述信息。
"""

def create_shortcut(bin_path: str, name: str, desc: str,start_in,icon_path):
    '''
    :param bin_path: exe路径
    :param name: 需要创建快捷方式的路径
    :param desc: 描述，鼠标放在图标上面会有提示
    :return:
    '''
    try:
        print(winshell.desktop())
        shortcut =  name + ".lnk"
        winshell.CreateShortcut(
            Path=shortcut,
            Target=bin_path,
            StartIn= start_in,
            Icon=(icon_path, 0),
            Description=desc
        )
        return True
    except ImportError as err:
        print("Well, do nothing as 'winshell' lib may not available on current os")
        print("error detail %s" % str(err))
    return False

def get_desktop():
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
                         r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
    return winreg.QueryValueEx(key, "Desktop")[0]

if __name__ == "__main__":
    # 将快捷方式添加到桌面
    deskTopDir = get_desktop()
    # deskTopDir = winshell.desktop()
    bin_path = r"./mainwindow/mainwindow.exe"

    start_in = os.path.abspath(r"./mainwindow")

    link_path_1 = deskTopDir + "\\Easy- Calendar"
    link_path_2 = os.path.abspath(r"./Easy- Calendar")

    icon_path =  os.path.abspath(r"./static/img/logo1.ico")

    desc = "日历"

    create_shortcut(bin_path, link_path_1, desc,start_in,icon_path)
    create_shortcut(bin_path, link_path_2, desc, start_in, icon_path)




